#include "qcomboitemeditdialog.h"
#include "ui_qcomboitemeditdialog.h"

#include "qaddcomboitemdialog.h"
#include "qcomboitemlistwidget.h"
#include "../qbaseitemdelegate.h"

#include "../../../libs/shared/qcommonstruct.h"
#include "../../../libs/core/qsoftcore.h"
#include "../../../libs/shared/qprojectcore.h"
#include "../../../libs/shared/resource/qresourcemanager.h"

#include <QItemDelegate>
#include <QPainter>
#include <QHeaderView>
#include <QIcon>
#include <QApplication>
#include <QVBoxLayout>
#include <QMetaMethod>
#include <QStringList>

QComboItemEditDialog::QComboItemEditDialog(const QVariant &value, QWidget *parent) :
    QBaseDialog(parent),
    ui(new Ui::QComboItemEditDialog),
    m_value(value),
    m_ret(0)
{
    ui->setupUi(this);

    ui->item_list->setRootIsDecorated(false);
    ui->item_list->setAlternatingRowColors(true);
    ui->item_list->setItemDelegate(new QBaseItemDelegate);

    ComboBoxItems items=m_value.value<ComboBoxItems>();

    foreach(tagComboBox item,items)
    {
        QTreeWidgetItem* it=new QTreeWidgetItem(ui->item_list);

        QComboItemListWidget *wid=new QComboItemListWidget(it,ui->item_list);
        wid->set_text(item.m_text);
        wid->set_icon(item.m_icon);
        connect(wid,SIGNAL(edit()),this,SLOT(edit()));
        connect(wid,SIGNAL(remove()),this,SLOT(remove()));
        connect(wid,SIGNAL(up()),this,SLOT(up()));
        connect(wid,SIGNAL(down()),this,SLOT(down()));
        ui->item_list->setItemWidget(it,0,wid);
        m_last_icon.append("");
    }
}

QComboItemEditDialog::~QComboItemEditDialog()
{
    delete ui;

    foreach(const QString& icon,m_last_icon)
    {
        if(icon!="")
        {
            QSoftCore::get_core()->get_project_core()->get_resource_manager()->removeResource(icon);
        }
    }
}

void QComboItemEditDialog::on_addBtn_clicked()
{
    ComboBoxItems items=m_value.value<ComboBoxItems>();

    QStringList list;

    foreach(tagComboBox item,items)
    {
        list.append(item.m_text);
    }

    QAddComboItemDialog dlg(list,"","",this);

    dlg.exec();

    QString text,icon;
    text=dlg.get_text();
    icon=dlg.get_icon();

    if(text!="")
    {
        tagComboBox it;
        it.m_text=text;
        it.m_icon=icon;

        items.append(it);
        m_value.setValue<ComboBoxItems>(items);

        QTreeWidgetItem *item=new QTreeWidgetItem(ui->item_list);
        QComboItemListWidget *wid=new QComboItemListWidget(item,ui->item_list);
        wid->set_text(it.m_text);
        wid->set_icon(it.m_icon);
        connect(wid,SIGNAL(edit()),this,SLOT(edit()));
        connect(wid,SIGNAL(remove()),this,SLOT(remove()));
        connect(wid,SIGNAL(up()),this,SLOT(up()));
        connect(wid,SIGNAL(down()),this,SLOT(down()));
        ui->item_list->setItemWidget(item,0,wid);
        m_last_icon.append(icon);
    }
}

void QComboItemEditDialog::on_okBtn_clicked()
{
    m_ret=1;
    close();
}

void QComboItemEditDialog::on_cancelBtn_clicked()
{
    close();
}

int QComboItemEditDialog::get_ret()
{
    return m_ret;
}

QVariant QComboItemEditDialog::get_value()
{
    return m_value;
}

void QComboItemEditDialog::edit()
{
    QComboItemListWidget* wid=(QComboItemListWidget*)sender();

    QTreeWidgetItem* item=wid->get_item();

    ComboBoxItems list=m_value.value<ComboBoxItems>();
    int index=ui->item_list->indexOfTopLevelItem(item);
    tagComboBox c=list.at(index);

    QStringList l;

    foreach(tagComboBox it,list)
    {
        l.append(it.m_text);
    }
    l.removeAll(c.m_text);

    QAddComboItemDialog dlg(l,c.m_text,c.m_icon,this);

    dlg.exec();

    if(dlg.get_text()!="")
    {
        if(m_last_icon.at(index)!=""  && m_last_icon.at(index)!=dlg.get_icon())
        {
            QSoftCore::get_core()->get_project_core()->get_resource_manager()->removeResource(m_last_icon.at(index));

        }
        m_last_icon.replace(index,dlg.get_icon());
        c.m_icon=dlg.get_icon();
        c.m_text=dlg.get_text();
        list.replace(index,c);
        wid->set_text(c.m_text);
        wid->set_icon(c.m_icon);
        m_value.setValue<ComboBoxItems>(list);
        ui->item_list->setCurrentItem(item);
    }
}

void QComboItemEditDialog::remove()
{
    QComboItemListWidget* wid=(QComboItemListWidget*)sender();

    QTreeWidgetItem* item=wid->get_item();

    ComboBoxItems list=m_value.value<ComboBoxItems>();
    int index=ui->item_list->indexOfTopLevelItem(item);

    list.removeAt(index);
    delete item;

    m_value.setValue<ComboBoxItems>(list);
    if(m_last_icon.at(index)!="")
    {
        QSoftCore::get_core()->get_project_core()->get_resource_manager()->removeResource(m_last_icon.at(index));
        m_last_icon.removeAt(index);
    }
}

void QComboItemEditDialog::up()
{
    QComboItemListWidget* wid=(QComboItemListWidget*)sender();

    QTreeWidgetItem* item=wid->get_item();

    ComboBoxItems list=m_value.value<ComboBoxItems>();
    int index=ui->item_list->indexOfTopLevelItem(item);
    tagComboBox c=list.at(index);

    list.swap(index,index-1);
    ui->item_list->takeTopLevelItem(index);
    ui->item_list->insertTopLevelItem(index-1,item);

    QComboItemListWidget *w=new QComboItemListWidget(item,ui->item_list);
    w->set_text(c.m_text);
    w->set_icon(c.m_icon);
    connect(w,SIGNAL(edit()),this,SLOT(edit()));
    connect(w,SIGNAL(remove()),this,SLOT(remove()));
    connect(w,SIGNAL(up()),this,SLOT(up()));
    connect(w,SIGNAL(down()),this,SLOT(down()));
    ui->item_list->setItemWidget(item,0,w);

    m_value.setValue<ComboBoxItems>(list);
    ui->item_list->setCurrentItem(item);
    m_last_icon.swap(index,index-1);
}

void QComboItemEditDialog::down()
{

    QComboItemListWidget* wid=(QComboItemListWidget*)sender();

    QTreeWidgetItem* item=wid->get_item();

    ComboBoxItems list=m_value.value<ComboBoxItems>();
    int index=ui->item_list->indexOfTopLevelItem(item);
    tagComboBox c=list.at(index);

    list.swap(index,index+1);
    ui->item_list->takeTopLevelItem(index);
    ui->item_list->insertTopLevelItem(index+1,item);

    QComboItemListWidget *w=new QComboItemListWidget(item,ui->item_list);
    w->set_text(c.m_text);
    w->set_icon(c.m_icon);
    connect(w,SIGNAL(edit()),this,SLOT(edit()));
    connect(w,SIGNAL(remove()),this,SLOT(remove()));
    connect(w,SIGNAL(up()),this,SLOT(up()));
    connect(w,SIGNAL(down()),this,SLOT(down()));
    ui->item_list->setItemWidget(item,0,w);
    m_value.setValue<ComboBoxItems>(list);
    ui->item_list->setCurrentItem(item);
    m_last_icon.swap(index,index+1);
}
